package com.beidasoft.web.util;

/**
 * Created by LiZhenbin on 2016/4/11.
 * 将数字转换成中文汉字
 */
public class ArabicToChineseUtils {

    static String[] units = {"", "十", "百", "千", "万", "十", "百万", "千万", "亿", "十亿", "百亿", "千亿", "万亿"};
    static char[] numArray = {'零', '一', '二', '三', '四', '五', '六', '七', '八', '九'};

    public static String formatInteger(int num) {
        String str = "";
        if (num != 0) {
            char[] val = String.valueOf(num).toCharArray();
            int len = val.length;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < len; i++) {
                String m = val[i] + "";
                int n = Integer.valueOf(m);
                boolean isZero = n == 0;
                String unit = units[(len - 1) - i];
                if (isZero) {
                    if ('0' == val[i - 1]) {
                        continue;
                    } else {
                        sb.append(numArray[n]);
                    }
                } else {
                    if (sb.length() == 0 && n == 1 && "十".equals(unit)) {
                        sb.append(unit);
                    } else {
                        sb.append(numArray[n]);
                        sb.append(unit);
                    }

                }
            }
            str = sb.toString();
            if (str.substring(str.length() - 1, str.length()).equals("零")) {
                str = str.toString().substring(0, str.length() - 1);
            }
        } else {
            str = "";
        }
        return str;
    }

}
